﻿@using System.Web.Script.Serialization
@using yiCommerce.Core.Domain.Orders
@using yiCommerce.Core.Domain.Payments
@using yiCommerce.Core.Domain.Shippings
@{
    ViewBag.Title = "List";
    Layout = "~/views/shared/_Layout.cshtml";
}


<h2>List</h2>


<!-- ko with: OrderSearchFilterModel -->

<table width="100%">
    <tr>
        <td class="adminTitle">
        </td>
        <td class="adminData">
            <input type="text" data-bind="value:StartDate"/>
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
        </td>
        <td class="adminData">
            <input type="text" data-bind="value:EndDate"/>
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
        </td>
        <td class="adminData">
                
            <input type="text" data-bind="value:CustomerEmail"/>
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
        </td>
        <td class="adminData">
             <select data-bind="options: AvailableOrderStatuses, optionsText: 'Text', value: OrderStatusId,optionsValue:'Value'"></select>


        </td>
    </tr>
    <tr>
        <td class="adminTitle">
        </td>
        <td class="adminData">
            
            <select data-bind="options: AvailablePaymentStatuses, optionsText: 'Text', value: PaymentStatusId,optionsValue:'Value'"></select>

        </td>
    </tr>
    <tr>
        <td class="adminTitle">
        </td>
        <td class="adminData">
            
            <select data-bind="options: AvailableShippingStatuses, optionsText: 'Text', value: ShippingStatusId,optionsValue:'Value'"></select>
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
        </td>
        <td class="adminData">
            
              <input type="text" data-bind="value:OrderGuid"/>
        </td>
    </tr>
   
    <tr>
        <td colspan="2">
                
            <input type="button" class="t-button" data-bind="click:$root.DoSearch"  value="Search"/>
        </td>
    </tr>
</table>
<!-- /ko -->

<div class="t-grid t-widget">
    <table>
        <thead class="t-grid-header">
            <tr><th class="t-header" scope="col">OrderId</th>
                <th class="t-header" scope="col" style="text-align:center">OrderStatus</th>
                <th class="t-header" scope="col"><span class="t-link">PaymentStatus</span></th>
                <th class="t-header" scope="col"><span class="t-link">ShippingStatus</span></th>
                <th class="t-header" scope="col" style="text-align:center;">CustomerEmail</th>
                <th class="t-header" scope="col" style="text-align:center;">CreatedOn</th>
                <th class="t-header" scope="col" style="text-align:center;"></th>

            </tr>
        </thead>

        <tbody>
        
            <!-- ko foreach: OrderListItems -->
            <tr>
                <td>
                    <span data-bind="text:Id"></span>
                </td>
                <td>
                    <span data-bind="text:OrderStatusStr"></span>
                </td>
                <td>
                    <span data-bind="text:PaymentStatusStr"></span>
                </td>
                <td>
                    <span data-bind="text:ShippingStatusStr"></span>
                </td>
                <td>
                    <span data-bind="text:CustomerEmail"></span>
                </td>
                <td>
                    <span data-bind="text:CreatedOn"></span>
                </td>
                <td>
                    <!-- ko if: PaymentStatus() == @((int)PaymentStatus.Pending) -->
                    <a data-bind="attr:{href:RePayUrl}" target="_blank">go pay</a>
                    <!-- /ko -->
                     <!-- ko if: ShippingStatus() == @((int)ShippingStatus.Shipped)  && OrderStatus() !=  @((int)OrderStatus.Complete)  -->
                    <a data-bind="attr:{href:ConfirmUrl}" target="_blank">comfirm received</a>
                    <!-- /ko -->
                </td>
            </tr>
            <!-- /ko -->
        </tbody>
    </table>
</div>

@{
    var model = ViewBag.Model;
    var jsonData = new HtmlString(new JavaScriptSerializer().Serialize(model));
}

@section scripts{
    


<script type="text/javascript">
    var initialData = @jsonData;

    function OrderListItemModel(id,orderStatusStr,paymentStatusStr,shippingStatusStr,customerEmail,createdOn,orderTotal,orderStatus, paymentStatus, shippingStatus) {
        this.Id= ko.observable(id);
        this.OrderStatusStr = ko.observable(orderStatusStr);
        this.PaymentStatusStr = ko.observable(paymentStatusStr);
        this.ShippingStatusStr = ko.observable(shippingStatusStr);
        this.CustomerEmail = ko.observable(customerEmail);
        this.CreatedOn = ko.observable(createdOn);
        this.OrderTotal = ko.observable(orderTotal);
        this.RePayUrl = ko.observable("/Order/RePostPayment?orderId=" + id);
        this.ConfirmUrl = ko.observable("/Order/Confirm?orderId=" + id);
        this.OrderStatus = ko.observable(orderStatus);
        this.PaymentStatus= ko.observable(paymentStatus);
        this.ShippingStatus= ko.observable(shippingStatus);

    }

    function OrderSearchFilterModel(startDate,
        endDate,
        customerEmail, 
        availableOrderStatus, 
        orderStatusId, 
        availablePaymentStatuses,
        paymentStatusId,
        availableShippingStatuses,
        shippingStatusId,
        orderGuid) 
    {
            
        this.StartDate = ko.observable(startDate);
        this.EndDate = ko.observable(endDate);
        this.CustomerEmail = ko.observable(customerEmail);


        this.AvailableOrderStatuses = ko.observableArray(availableOrderStatus);
        this.OrderStatusId = ko.observable(orderStatusId);


        this.AvailablePaymentStatuses = ko.observableArray(availablePaymentStatuses);
        this.PaymentStatusId = ko.observable(paymentStatusId);
            
        this.AvailableShippingStatuses = ko.observableArray(availableShippingStatuses);
        this.ShippingStatusId = ko.observable(shippingStatusId);

        this.OrderGuid = ko.observable(orderGuid);
            
        //this.pageIndex = ko.observable(1);
        //this.pageSize = ko.observable(30);
    }

    function OrderListModel() {
        var self = this;

        self.OrderSearchFilterModel = new OrderSearchFilterModel(initialData.Filter.StartDate, 
            initialData.Filter.EndDate,  
            initialData.Filter.CustomerEmail, 
            initialData.Filter.AvailableOrderStatuses, 
            initialData.Filter.OrderStatusId,
            initialData.Filter.AvailablePaymentStatuses,
            initialData.Filter.PaymentStatusId,
            initialData.Filter.AvailableShippingStatuses,
            initialData.Filter.ShippingStatusId,
            ""
        );
        var items = $.map(initialData.Orders, function (a) {
            return new OrderListItemModel(a.Id, a.OrderStatusStr, a.PaymentStatusStr, a.ShippingStatusStr, a.CustomerEmail, a.CreatedOn, a.OrderTotal, a.OrderStatus, a.PaymentStatus, a.ShippingStatus);
        }); 
            
        self.OrderListItems = ko.observableArray(items);
        self.pageIndex = ko.observable(0);
        self.pageSize= ko.observable(30); 
        self.DoSearch = function() {
            $.ajax({
                url: "/api/OrderAdminApi/SearchOrders",
                type: 'post',
                data:  ko.toJSON(self.OrderSearchFilterModel),
                contentType: 'application/json',
                success: function (result) {
                    self.OrderListItems.removeAll();
                    
                    var tempItems= $.map(result.Orders, function (a) {
                        return new OrderListItemModel(a.Id, a.OrderStatusStr, a.PaymentStatusStr, a.ShippingStatusStr, a.CustomerEmail, a.CreatedOn, a.OrderTotal, a.OrderStatus, a.PaymentStatus, a.ShippingStatus);
 
                    });
                    self.OrderListItems(tempItems);
                }
            });
                
        };

    }

    ko.applyBindings(new OrderListModel());   
        

</script>
    }